package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.FormDesignConstant;
import com.aplink.generic.constant.MenuConstant;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.MenuShowAsActionType;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.util.objectrule.TargetCollection;
import com.aplink.generic.util.validation.LocationValidation;

public class MenuItemParser {
    public static final String MESSAGE = "message";

    public static final String VALID_LOCATION = "validlocation";

    private static String getViewTitile(final String viewTitle) {
        return ResourcesHelper.getStringByName(viewTitle);
    }

    public static MenuItemSpec parseMenuItemSpec(final Node nodeItem) {
        final MenuItemSpec popupMenuItem = new MenuItemSpec();
        if (nodeItem.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(nodeItem,
                    MenuConstant.TITLE);
            if (!TextUtils.isEmpty(value)) {
                popupMenuItem.setTitle(MenuItemParser.getViewTitile(value));
            }

            value = XMLPaserUtil.getAttrValue(nodeItem, MenuConstant.ID);
            if (!TextUtils.isEmpty(value)) {
                popupMenuItem.setID(value);
            }

            value = XMLPaserUtil.getAttrValue(nodeItem, MenuConstant.ACTION);
            if (!TextUtils.isEmpty(value)) {
                final ActionType actionType = ActionType.get(value);
                popupMenuItem.setActionType(actionType);
            }

            value = XMLPaserUtil.getAttrValue(nodeItem, MenuConstant.ICON);
            if (!TextUtils.isEmpty(value)) {
                popupMenuItem.setIcon(value);
            }

            value = XMLPaserUtil.getAttrValue(nodeItem,
                    MenuConstant.ORDER_IN_CATEGORY);
            if (!TextUtils.isEmpty(value)) {
                popupMenuItem.setOrderInCategory(Integer.parseInt(value));
            }

            value = XMLPaserUtil.getAttrValue(nodeItem,
                    MenuConstant.SHOW_AS_ACTION);
            if (!TextUtils.isEmpty(value)) {
                popupMenuItem.setShowAsActionType(MenuShowAsActionType
                        .get(value));
            }

        }
        final Element validationElement = (Element) nodeItem;
        final NodeList validationNodeList = validationElement
                .getElementsByTagName(FormDesignConstant.VALIDATIONGROUP);
        if (validationNodeList.getLength() != 0) {
            final String validationGroup = XMLPaserUtil
                    .getNodeValue(validationNodeList.item(0));
            popupMenuItem.setValidationGroup(validationGroup);
        }
        final TargetCollection targetCollection = TargetCollectionPaser
                .parse(validationElement);
        popupMenuItem.setTargetCollection(targetCollection);
        popupMenuItem.setLocationValidation(MenuItemParser
                .parseValidLocation(validationElement));
        return popupMenuItem;
    }

    private static LocationValidation parseValidLocation(
            final Element elementFormat) {
        final NodeList nodeListFormat = elementFormat
                .getElementsByTagName(MenuItemParser.VALID_LOCATION);
        Node node = null;
        String value = null;
        if (nodeListFormat.getLength() == 1) {
            node = nodeListFormat.item(0);
            final LocationValidation duplicate = new LocationValidation();
            if (node.hasAttributes()) {
                value = ResourcesHelper.getStringByName(XMLPaserUtil
                        .getAttrValue(node, MenuItemParser.MESSAGE));
                duplicate.setMessage(value);
            }
            return duplicate;
        }
        return null;
    }
}
